<?php
/*
* Author: Nguyen Hai Nam
* Module: CountriesModule
* Desc	: Introduce pick.vn and sidle pic
* Date	: 12/12/2011
*/
class CountriesModule extends Pick_Core_Module{
	
	public $authentication = 1;
	
	function CountriesModule(){
		
		parent::Pick_Core_Module();
		$this->theme = "admin";
		# Languages
		$languages = new Pick_Core_Languages();
		$objectLanguages = "";
		$objectLanguages = $languages->getObjects(1, 'status = 1', array('position'=>'ASC'), '');
		if($objectLanguages){
			$objectLanguages = $objectLanguages['data'];	
		}#endif
		$this->template->assign("objectLanguages", $objectLanguages);
        
        // Get Path for this module
        $this->template->assign('arrayParentPath', $this->getArrayParentPath());
	}
	public function getContent(){
		
		$act = $this->request->element('act', "list");
		$this->template->assign('act', $act);
		if($act == "position"){
			$countries 	= new Pick_Dao_Countries();
			$oId = $this->request->element('id','');
			$val = $this->request->element('val','');
			$arrData = array('position'=>$val);
			echo $countries->updateData($arrData, $oId);
			die;
		}
		if($act == "add"){
			$this->addAction();
		}else{
			$this->listAction();	
		}
		
	}
	public function listAction(){
		
		$countries 		= new Pick_Dao_Countries();
		$actions 		= new Pick_Core_Actions();
		$actions->setInfoAction($countries, $this->request);
		
		$items_per_pages = DEFAULT_ADMIN_ROWS_PER_PAGE;
		$infoText = '';
		$infoClass = 'infoText';
		
		# Read parameters from request
		$plus 			= $this->request->element('plus','');
		$searchTerms 	= $this->request->element('searchTerms','');
		$searchStatus 	= $this->request->element('searchStatus','-1');
		$page 			= $this->request->element('page',1);
		$orderBy 		= $this->request->element('orderBy','position');
		$orderDir 		= $this->request->element('orderDir','ASC');
		
		# Control plus action
		if($plus){
			$actions->auto($plus);
			$infoText 	= $actions->getInfoAction();
			$infoClass 	= "errorText";		
		}
		
		# Build search condition
		$searchCond = '1=1';
		if($searchStatus != '-1') 	$searchCond .= " AND status = '$searchStatus'";
		if($searchTerms) 			$searchCond .= " AND name LIKE '%".$searchTerms."%'";
		
		# Get all countries object and pass to template
		$listObjects = $countries->getObjects($page, $searchCond, array($orderBy => $orderDir), $items_per_pages);
		
		# Generate status combobox for Search form
		$listStatus = optionStatus($searchStatus);

		
		# Pass some variables to template
		$this->template->assign('page', $page);
		$this->template->assign('listStatus', $listStatus);
		$this->template->assign('listObjects', $listObjects);
		$this->template->assign('searchTerms', $searchTerms);
		$this->template->assign('searchStatus', $searchStatus);
		$this->template->assign('orderBy', $orderBy);
		$this->template->assign('orderDir', $orderDir);
		$this->template->assign('infoText', $infoText);
		$this->template->assign('infoClass', $infoClass);

	}
	
	public function addAction(){
		
		$countries 		= new Pick_Dao_Countries();
		$objectInfo 	= new Pick_Dao_CountriesInfo();
		$textfilter 	= new Pick_Data_TextFilter();
		 
		$infoText 	= '';
		$infoClass 	= '';
		$oId 		= $this->request->element('oId',0);
		$plus 		= $this->request->element('plus','');
		
		# Read parameters from request
		$object 	= $countries->getObject($oId);
		$arrayFieldFromMySQL = $countries->getListFields();
		if(is_object($object) && $this->request->post('plus')==""){
			foreach($arrayFieldFromMySQL as $fieldName){
				$fields[$fieldName] = $object->getField($fieldName);
			}#endforeach	
		}else{
			foreach($arrayFieldFromMySQL as $fieldName){
				$fields[$fieldName] = $this->request->element($fieldName,'');
			}#endforeach
		}
		#Modify $fields
		$fields['slug'] = $textfilter->urlize($fields[$this->lang.'_name']);
		
		# Gan gia tri cho doi tuong
		$objectInfo->setArrayField($fields);

		
		# Truy du lieu qua template
		$this->template->assign('listStatus', optionStatus($objectInfo->getField('status'), DEFAULT_ADMIN_LANGUAGE));
		
		foreach($arrayFieldFromMySQL as $fieldName){
			$this->template->assign($fieldName, $objectInfo->getField($fieldName));
		}#endforeach
		
		// Bat dau qua trinh su li du lieu
		if($_POST && $plus == 'saveItem') {
			# Validation
			$validate 	= new Pick_Data_Validate();
			$validatemess = $this->validateData($this->request, $validate);
			
			if($validatemess == '') {
				/* Kiem tra neu mau tin da duoc them thi khong cho them nua */
				$object = "";
				
				if($oId<=0){// Truong hop them thi moi kiem tra slug
					$object = $countries->getObject($objectInfo->getField('id'));
				}
				if(is_object($object)){
					$infoText = $this->l('item_saved_error');	
					$infoClass = "errorText";							
				}else{
					$ok = $countries->updateData($objectInfo->getArrayField(), $oId);
					if($ok){
						$infoText = $this->l('item_save_ok');
						$oId = $oId?$oId:mysql_insert_id();
						$infoClass = "InfoText";			
					}	
				}	
					
			}else{
				$infoText = $this->l('error').":<br />".$validatemess;
				$infoClass = "errorText";		
			}
		}
		
		$this->template->assign('oId', $oId);
		$this->template->assign('objectInfo', $objectInfo);		
		$this->template->assign('infoText', $infoText);
		$this->template->assign('infoClass', $infoClass);			

	}
	
	function validateData($request, $validate) {
		$infoText = '';
		$infoText .= $validate->validString($this->l('code'), $request->element("code"));
		$infoText .= $validate->validString($this->l('name'), $request->element($this->lang."_name"));		
		return $infoText;
	}
}